(function() {

  window.app = {};

  app.initialize = function(data) {
    app.school_id = data.si;
    app.token = data.tk;
    return $('#school-name').text(data.sn);
  };

}).call(this);

(function() {
  var $school_languages, confirm_add_language, hide_add_language, show_language, start_add_language, try_save_language;

  window.language = {};

  $school_languages = {};

  start_add_language = function() {
    return $('#add-language').modal();
  };

  confirm_add_language = function() {
    var la;
    console.log("Confirming add language");
    la = $('#language-select').val();
    $school_languages[la] = [];
    if ($('#101-checkbox').attr('checked')) $school_languages[la].push('101');
    if ($('#201-checkbox').attr('checked')) $school_languages[la].push('201');
    if ($('#301-checkbox').attr('checked')) $school_languages[la].push('301');
    return try_save_language(la, function() {
      return show_language(la, $school_languages[la]);
    });
  };

  show_language = function(la, cl) {
    var tbody;
    tbody = "";
    if (_.contains(cl, '101')) tbody += "<tr><td>101</td></tr>";
    if (_.contains(cl, '201')) tbody += "<tr><td>201</td></tr>";
    if (_.contains(cl, '301')) tbody += "<tr><td>301</td></tr>";
    if ($("#" + la + "-display").length > 0) $("" + la + "-display").remove();
    return $('#class-display').prepend("<table id=\"" + la + "-display\" class=\"table-bordered span2 textcenter\" style=\"height:300px\">\n  <thead><tr><th>" + la + "</th></tr></thead>\n  <tbody>\n    " + tbody + "\n    <tr><td><a class=\"btn btn-dark btn-small\" onclick=\"language.remove('" + la + "')\">Remove</a></td></tr>\n  </tbody>\n</table>");
  };

  try_save_language = function(la, callback) {
    return $.post('/school/languages/save', {
      i: app.school_id,
      tk: app.token,
      la: la,
      cl: $school_languages[la].join(',')
    }, function(data) {
      if (data.error) {
        $.meow({
          message: "The server doesn't like your values ... Refresh the page and try again, and if that doesn't work <a target='_blank' href='/contact'>contact us</a>."
        });
      } else {
        callback();
      }
      return hide_add_language();
    });
  };

  hide_add_language = function() {
    console.log("Hiding add language");
    return $('#add-language').modal('hide');
  };

  language.remove = function(la) {
    return $.post('/school/languages/remove', {
      i: app.school_id,
      tk: app.token,
      la: la
    }, function(data) {
      if (data.error) {
        return $.meow({
          message: "Hmm, removing that language didn't work ... Refresh the page and try again, and if that doesn't work <a target='_blank' href='/contact'>contact us</a>."
        });
      } else {
        return $("#" + la + "-display").remove();
      }
    });
  };

  language.initialize = function(data) {
    var sl, _i, _len, _ref;
    _ref = data.sl;
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      sl = _ref[_i];
      show_language(LANGUAGE_NAME[sl.la], sl.cl);
    }
    $('#add-language-btn').click(start_add_language);
    $('#confirm-language-btn').click(confirm_add_language);
    return $('#cancel-language-btn').click(hide_add_language);
  };

}).call(this);

(function() {

  window.navigation = {};

  navigation.initialize = function() {
    return $('#main-nav li a').click(function() {
      var target;
      $('#main-nav li').removeClass('active');
      $(this).parent().addClass('active');
      $('.content-pane').hide();
      target = this.innerHTML.toLowerCase();
      $("#" + target).fadeIn();
      if (window[target] && window[target].onSwitch) {
        return window[target].onSwitch();
      }
    });
  };

}).call(this);

(function() {
  var $data_view, $email_suffix, $grid, $name_pattern, ClassesCellFormatter, generate_email_example, generate_name_example, refresh_name_pattern, setup_grid, setup_settings;

  window.students = {};

  $grid = null;

  $data_view = null;

  $name_pattern = '\\l{6}\\f{1}';

  $email_suffix = 'you.school.nz';

  ClassesCellFormatter = function(row, cell, value, columnDef, dataContext) {
    var language, text, _i, _len;
    text = "";
    for (_i = 0, _len = value.length; _i < _len; _i++) {
      language = value[_i];
      text += "" + language.la + " " + language.cl + ", ";
    }
    return text.slice(-2);
  };

  setup_grid = function(students) {
    var columns;
    columns = [
      {
        id: "un",
        title: "Username",
        sortable: true
      }, {
        id: "rn",
        title: "Real Name",
        sortable: true
      }, {
        id: "cl",
        title: "Classes",
        sortable: false,
        formatter: ClassesCellFormatter
      }
    ];
    return $grid = $('#students-grid').powergrid(columns, students);
  };

  setup_settings = function(name_pattern, email_suffix) {
    var match;
    if (name_pattern === "") $('#set-s-username-pattern').hide();
    if (email_suffix === "") $('#set-s-email-suffix').hide();
    if (!(name_pattern && email_suffix)) return;
    $name_pattern = name_pattern;
    $email_suffix = email_suffix;
    generate_name_example($name_pattern);
    generate_email_example($email_suffix);
    if ((match = name_pattern.match(/^\\(f|l)\{([0-9]+)\}/))) {
      if (match[1] === 'f') {
        $('#s-pattern-s1 option').removeAttr('selected');
        $('#s-pattern-s1 option').first().attr('selected', true);
      }
      $('#s-pattern-n1').val(match[2]);
    }
    if ((match = name_pattern.match(/\\(f|l)\{([0-9]+)\}$/))) {
      if (match[1] === 'f') {
        $('#s-pattern-s2 option').removeAttr('selected');
        $('#s-pattern-s2 option').first().attr('selected', true);
      }
      return $('#s-pattern-n2').val(match[2]);
    }
  };

  refresh_name_pattern = function() {
    var i, val;
    $name_pattern = "";
    for (i = 1; i <= 2; i++) {
      if ($("#s-pattern-s" + i).val() === "First Name") {
        $name_pattern += '\\f';
      } else {
        $name_pattern += '\\l';
      }
      if ((val = parseInt($("#s-pattern-n" + i).val())) > 0) {
        $name_pattern += "{" + val + "}";
      } else {
        $name_pattern += "{0}";
      }
    }
    return generate_name_example($name_pattern);
  };

  generate_name_example = function(pattern) {
    var firstname, lastname;
    console.log(pattern);
    firstname = "timothy";
    lastname = "hampton";
    pattern.match(/\\f\{([0-9]+)\}/);
    pattern = pattern.replace(/\\f\{([0-9]+)\}/, firstname.substring(0, RegExp.$1));
    pattern.match(/\\l\{([0-9]+)\}/);
    pattern = pattern.replace(/\\l\{([0-9]+)\}/, lastname.substring(0, RegExp.$1));
    $('#s-username-example').text(app.school_id + '.' + pattern);
    return generate_email_example($email_suffix);
  };

  generate_email_example = function(suffix) {
    var username;
    username = $('#s-username-example').text();
    return $('#s-email-example').text(username.substr(4) + '@' + suffix);
  };

  students.onSwitch = function() {
    console.log($grid);
    return $grid.powerlayout();
  };

  students.initialize = function(data) {
    var i;
    console.log(data);
    setup_grid(data.st);
    setup_settings(data.sp, data.se);
    $('#student-settings-btn').click(function() {
      return $('#student-settings').modal('show');
    });
    $('#snp-choice a').click(function() {
      if (this.innerHTML.indexOf("Pattern") !== -1) {
        $('#set-s-username-pattern').show();
        return refresh_name_pattern();
      } else {
        $('#set-s-username-pattern').hide();
        return $name_pattern = null;
      }
    });
    $('#ses-choice a').click(function() {
      if (this.innerHTML.indexOf("Pattern") !== -1) {
        $('#set-s-email-suffix').show();
        $email_suffix = $('#student-email-suffix').val();
        return generate_email_example($email_suffix);
      } else {
        $('#set-s-email-suffix').hide();
        return $email_suffix = null;
      }
    });
    for (i = 1; i <= 2; i++) {
      $("#s-pattern-s" + i).change(refresh_name_pattern);
      $("#s-pattern-n" + i).change(refresh_name_pattern);
    }
    $('#student-email-suffix').change(function() {
      $email_suffix = $(this).val();
      return generate_email_example($email_suffix);
    });
    $('#student-settings-ok').click(function() {
      $('#student-settings-ok').button('loading');
      return $.post('/school/settings/student/save', {
        i: app.school_id,
        tk: app.token,
        p: $name_pattern || "",
        e: $email_suffix || ""
      }, function(data) {
        if (data.error) {
          $.meow({
            message: "The server doesn't like your values ... Check the form is valid, and if it is then <a target='_blank' href='/contact'>contact us!</a>"
          });
        } else {
          $('#student-settings').modal('hide');
        }
        return $('#ok-student-settings').button('reset');
      });
    });
    $('#student-settings-cancel').click(function() {
      return $('#student-settings').modal('hide');
    });
    $('#add-student-i-btn').click(function() {
      return $('#add-student-i').modal('show');
    });
    return $('#cancel-student-i').click(function() {
      return $('#add-student-i').modal('hide');
    });
  };

}).call(this);

(function() {

  window.initialize = function(data) {
    common.initialize();
    app.initialize(data);
    navigation.initialize(data);
    language.initialize(data);
    return students.initialize(data);
  };

}).call(this);
